import 'package:flutter/material.dart';
import 'package:todo_project/services/settings_service.dart';

class ThemeNotifier extends ChangeNotifier {
  final SettingsService _settingsService = SettingsService();

  Color _primaryColor = const Color(0xFF6366F1); // Default color
  double _fontSizeScaleFactor = 1.0; // Default scale factor
  bool _isInitialized = false;

  ThemeNotifier() {
    _initializeAsync();
  }

  Color get primaryColor => _primaryColor;
  double get fontSizeScaleFactor => _fontSizeScaleFactor;

  void _initializeAsync() {
    if (_isInitialized) return;
    _isInitialized = true;
    
    initialize();
  }

  Future<void> initialize() async {
    try {
      _primaryColor = await _settingsService.loadThemeColor();
      _fontSizeScaleFactor = await _settingsService.loadFontSizeScaleFactor();
      notifyListeners();
    } catch (e) {
      print('Failed to load theme settings: $e');
      // Keep default values if loading fails
    }
  }

  Future<void> setPrimaryColor(Color color) async {
    _primaryColor = color;
    try {
      await _settingsService.saveThemeColor(color);
    } catch (e) {
      print('Failed to save theme color: $e');
    }
    notifyListeners();
  }

  Future<void> setFontSizeScaleFactor(double scaleFactor) async {
    _fontSizeScaleFactor = scaleFactor;
    try {
      await _settingsService.saveFontSizeScaleFactor(scaleFactor);
    } catch (e) {
      print('Failed to save font scale factor: $e');
    }
    notifyListeners();
  }
} 